package com.xuexiang.xuidemo.slice.tabbar.tabsegment;

import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ListContainer;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.element.PixelMapElement;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;

import com.xuexiang.xui_lib.component.dialog.config.ChooseBean;
import com.xuexiang.xui_lib.component.dialog.config.ConfigBean;
import com.xuexiang.xui_lib.component.dialog.interfaces.ItemDialogListener;
import com.xuexiang.xui_lib.component.dialog.view.StyledDialog;
import com.xuexiang.xui_lib.component.tabbar.TabSegment;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.provider.DemoDataProvider;
import com.xuexiang.xuidemo.provider.NewsCardViewListProvider;
import com.xuexiang.xuidemo.provider.entity.NewInfo;
import com.xuexiang.xuidemo.slice.BaseAbilitySlice;
import com.xuexiang.xuidemo.slice.tabbar.tablayout.ContentPage;
import com.xuexiang.xuidemo.util.WebUtils;
import com.xuexiang.xuidemo.util.XToastUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TabSegmentFixModeSlice extends BaseAbilitySlice {
    private TabSegment mTabSegment1;
    private TabSegment mTabSegment;
    private PageSlider mContentViewPager;

    private String[] pages = ContentPage.getPageNames();

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_slice_tab_segment_fix_mode;
    }

    protected void onTitleBarRightClick() {
        showBottomSheetList();
    }

    @Override
    protected void initComponents() {
        findComponentById(ResourceTable.Id_tab_image_back).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
        findComponentById(ResourceTable.Id_tab_text_right).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showBottomSheetList();
            }
        });
        mTabSegment = (TabSegment) findComponentById(ResourceTable.Id_tabSegment);
        mTabSegment1 = (TabSegment) findComponentById(ResourceTable.Id_tabSegment1);
        mContentViewPager = (PageSlider) findComponentById(ResourceTable.Id_contentViewPager);

//        initViewPagerTabSegment();
        initNoViewPagerTabSegment();

        new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
            @Override
            public void run() {
                initViewPagerTabSegment();
            }
        }, 300);
    }

    private void initViewPagerTabSegment() {
        PageSliderProvider adapter = new TestPagerProvider();
        for (String page : pages) {
            mTabSegment.addTab(new TabSegment.Tab(page));
        }
        mContentViewPager.setProvider(adapter);
        mContentViewPager.setCurrentPage(0, false);
        mContentViewPager.setReboundEffect(false);
        mTabSegment.setMode(TabSegment.MODE_FIXED);
        mTabSegment.setupWithViewPager(mContentViewPager, false);
    }

    /**
     * 不使用ViewPager的情况
     */
    private void initNoViewPagerTabSegment() {
        for (String page : pages) {
            mTabSegment1.addTab(new TabSegment.Tab(page));
        }
        mTabSegment1.setMode(TabSegment.MODE_FIXED);
        mTabSegment1.setOnTabClickListener(index -> XToastUtils.toast("点击了" + index));
        //不使用ViewPager的话，必须notifyDataChanged，否则不能正常显示
        mTabSegment1.notifyDataChanged();
        mTabSegment1.selectTab(0);
        mTabSegment1.addOnTabSelectedListener(new TabSegment.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int index) {
                if (mTabSegment1 != null) {
                    mTabSegment1.hideSignCountView(index);
                }
            }

            @Override
            public void onTabUnselected(int index) {

            }

            @Override
            public void onTabReselected(int index) {
                if (mTabSegment1 != null) {
                    mTabSegment1.hideSignCountView(index);
                }
            }

            @Override
            public void onDoubleTap(int index) {

            }
        });
    }

    public class TestPagerProvider extends PageSliderProvider {
        private List<NewInfo> list;

        public TestPagerProvider() {
            this.list = DemoDataProvider.getDemoNewInfos();
        }

        @Override
        public int getCount() {
            return pages.length;
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int i) {
            ListContainer listContainer = new ListContainer(getContext());
            NewsCardViewListProvider sampleItemProvider = new NewsCardViewListProvider(list);
            listContainer.setItemProvider(sampleItemProvider);
            componentContainer.addComponent(listContainer);
            listContainer.setLayoutConfig(new ComponentContainer.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_PARENT,
                ComponentContainer.LayoutConfig.MATCH_PARENT));
            listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
                @Override
                public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
//                    Intent intent = new Intent();
//                    intent.setParam("url", list.get(i).getDetailUrl());
//                    present(new TabSegmentWebSlice(), intent);
                    WebUtils.goWeb(getAbility(),list.get(i).getDetailUrl());
                }
            });
            return listContainer;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
            componentContainer.removeComponent((Component) o);
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object o) {
            return true;
        }
    }

    private ArrayList<ChooseBean> getList() {
        ArrayList<ChooseBean> list = new ArrayList<>();
        ChooseBean chooseBean = new ChooseBean();
        chooseBean.setTxt("简单文字");
        list.add(chooseBean);

        ChooseBean chooseBean1 = new ChooseBean();
        chooseBean1.setTxt("文字 + 底部 indicator");
        list.add(chooseBean1);


        ChooseBean chooseBean2 = new ChooseBean();
        chooseBean2.setTxt("文字 + 顶部 indicator");
        list.add(chooseBean2);


        ChooseBean chooseBean3 = new ChooseBean();
        chooseBean3.setTxt("文字 + indicator 长度不要跟随内容长度");
        list.add(chooseBean3);

        ChooseBean chooseBean4 = new ChooseBean();
        chooseBean4.setTxt("文字 + icon(支持四个方向) + 自动着色选中态 icon");
        list.add(chooseBean4);

        ChooseBean chooseBean5 = new ChooseBean();
        chooseBean5.setTxt("显示红点");
        list.add(chooseBean5);

        ChooseBean chooseBean6 = new ChooseBean();
        chooseBean6.setTxt("选中态更换 icon");
        list.add(chooseBean6);

        ChooseBean chooseBean7 = new ChooseBean();
        chooseBean7.setTxt("不同 item，不同文字(icon)颜色");
        list.add(chooseBean7);

        ChooseBean chooseBean8 = new ChooseBean();
        chooseBean8.setTxt("根据 index 更新 tab 文案");
        list.add(chooseBean8);

        ChooseBean chooseBean9 = new ChooseBean();
        chooseBean9.setTxt("根据 index 完全替换 tab");
        list.add(chooseBean9);
        return list;
    }

    ConfigBean configBean;

    private void showBottomSheetList() {
        ArrayList<ChooseBean> list = getList();
        configBean = StyledDialog.buildMdIMAGEList(getContext(), list, new ItemDialogListener() {
            @Override
            public void onItemClick(CharSequence text, int position) {
                configBean.commonDialog.hide();
                switch (position) {
                    case 0:
                        mTabSegment1.reset();
                        mTabSegment1.setHasIndicator(false);
                        for (String page : pages) {
                            mTabSegment1.addTab(new TabSegment.Tab(page));
                        }
                        break;
                    case 1:
                        mTabSegment1.reset();
                        mTabSegment1.setHasIndicator(true);
                        mTabSegment1.setIndicatorPosition(false);
                        mTabSegment1.setIndicatorWidthAdjustContent(true);
                        for (String page : pages) {
                            mTabSegment1.addTab(new TabSegment.Tab(page));
                        }
                        break;
                    case 2:
                        mTabSegment1.reset();
                        mTabSegment1.setHasIndicator(true);
                        mTabSegment1.setIndicatorPosition(true);
                        mTabSegment1.setIndicatorWidthAdjustContent(true);
                        for (String page : pages) {
                            mTabSegment1.addTab(new TabSegment.Tab(page));
                        }
                        break;
                    case 3:
                        mTabSegment1.reset();
                        mTabSegment1.setHasIndicator(true);
                        mTabSegment1.setIndicatorPosition(false);
                        mTabSegment1.setIndicatorWidthAdjustContent(false);
                        for (String page : pages) {
                            mTabSegment1.addTab(new TabSegment.Tab(page));
                        }
                        break;
                    case 4:
                        mTabSegment1.reset();
                        mTabSegment1.setHasIndicator(false);
                        Resource componentRes = null;
                        Resource toolsRes = null;
                        try {
                            componentRes = getResourceManager().getResource(ResourceTable.Media_icon_tabbar_component);
                            toolsRes = getResourceManager().getResource(ResourceTable.Media_icon_tabbar_util);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (NotExistException e) {
                            e.printStackTrace();
                        }
                        PixelMapElement componentElement = new PixelMapElement(componentRes);
                        TabSegment.Tab component = new TabSegment.Tab(
                            componentElement,
                            null,
                            "组件", true
                        );
                        PixelMapElement toolsElement = new PixelMapElement(toolsRes);
                        TabSegment.Tab util = new TabSegment.Tab(
                            toolsElement,
                            null,
                            "工具", true
                        );
                        mTabSegment1.addTab(component);
                        mTabSegment1.addTab(util);
                        break;
                    case 5:
                        TabSegment.Tab tab = mTabSegment1.getTab(0);
                        tab.setSignCountMargin(0, -AttrHelper.vp2px(4, getContext()));
                        tab.showSignCountView(getContext(), 1);
                        break;
                    case 6:
                        mTabSegment1.reset();
                        mTabSegment1.setHasIndicator(false);
                        Resource cNor = null;
                        Resource cSel = null;
                        Resource utilNor = null;
                        Resource utilSel = null;
                        try {
                            cNor = getResourceManager().getResource(ResourceTable.Media_icon_tabbar_component);
                            cSel = getResourceManager().getResource(ResourceTable.Media_icon_tabbar_component_selected);
                            utilNor = getResourceManager().getResource(ResourceTable.Media_icon_tabbar_util);
                            utilSel = getResourceManager().getResource(ResourceTable.Media_icon_tabbar_util_selected);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (NotExistException e) {
                            e.printStackTrace();
                        }

                        TabSegment.Tab component2 = new TabSegment.Tab(
                            new PixelMapElement(cNor),
                            new PixelMapElement(cSel),
                            "组件", false
                        );
                        TabSegment.Tab util2 = new TabSegment.Tab(
                            new PixelMapElement(utilNor),
                            new PixelMapElement(utilSel),
                            "工具", false
                        );
                        mTabSegment1.addTab(component2);
                        mTabSegment1.addTab(util2);
                        break;
                    case 7:
                        mTabSegment1.reset();
                        mTabSegment1.setHasIndicator(true);
                        mTabSegment1.setIndicatorWidthAdjustContent(true);
                        mTabSegment1.setIndicatorPosition(false);
                        Resource cNor1 = null;
                        Resource utilNor1 = null;
                        try {
                            cNor1 = getResourceManager().getResource(ResourceTable.Media_icon_tabbar_component);
                            utilNor1 = getResourceManager().getResource(ResourceTable.Media_icon_tabbar_util);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (NotExistException e) {
                            e.printStackTrace();
                        }
                        TabSegment.Tab component3 = new TabSegment.Tab(
                            new PixelMapElement(cNor1),
                            null,
                            "组件", true
                        );
                        component3.setTextColor(getColor(ResourceTable.Color_colorPrimary),
                            getColor(ResourceTable.Color_xui_config_color_red));
                        TabSegment.Tab util3 = new TabSegment.Tab(
                            new PixelMapElement(utilNor1),
                            null,
                            "工具", true
                        );
                        util3.setTextColor(getColor(ResourceTable.Color_xui_config_color_gray_1),
                            getColor(ResourceTable.Color_xui_config_color_red));
                        mTabSegment1.addTab(component3);
                        mTabSegment1.addTab(util3);
                        break;
                    case 8:
                        mTabSegment1.updateTabText(0, "动态更新文案");
                        break;
                    case 9:
                        Resource resource = null;
                        try {
                            resource = getResourceManager().getResource(ResourceTable.Media_icon_tabbar_component);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (NotExistException e) {
                            e.printStackTrace();
                        }
                        TabSegment.Tab component4 = new TabSegment.Tab(
                            new PixelMapElement(resource),
                            null,
                            "动态更新", true
                        );
                        mTabSegment1.replaceTab(0, component4);
                        break;

                    default:
                        break;
                }
                mTabSegment1.notifyDataChanged();
            }

            @Override
            public void positiveButton() {
            }

            @Override
            public void negativeButton() {
            }
        });
        configBean.commonDialog.show();
    }
}
